
(function (exports) {

    var $ = null;

    var signBaseData = null;

    exports.SignLayer = cc.Layer.extend({
        addListener: function (code, listener) {
            network.addListener(code, listener, this);
        },
        onEnter: function () {
            var that = this;

            cc.Layer.prototype.onEnter.call(this);

            cc.loader.loadTxt(SignInCfg.proto, function (err, txt) {
                var protoBuf = dcodeIO.ProtoBuf;
                mProto.builder['SignIn'] = protoBuf.loadProto(txt);
                mProto.initCommonModule('SignIn');
                network.send(1001,
                    {userid: gameData.uid+"", gameid: SignInCfg.gameid, configid: 'sevendays', clientversion: "0"}
                );
            });

            //签到基础信息返回
            this.addListener(1002, function (data) {
                hideLoading();
                if(data.errCode == 0){
                    signBaseData = data;
                    that.init(data);
                }else{
                    showAlert("数据异常！");
                }
            });

            //签到返回
            this.addListener(1004, function (data) {
                if(data.errCode != 0){
                    showAlert(data.errMsg);
                }else{
                    network.send(3013, {});
                    that.updateSign(data);
                }
            });
        },
        
        onExit: function () {
            network.removeListenersForTarget(this);
            cc.Layer.prototype.onExit.call(this);
        },

        ctor: function () {
            this._super();

            var self = this;

            loadNodeCCS(res.SignInLayer_json, this);
            $ = create$(this.getChildByName("Scene"));

            for(var i=0;i<7;i++){
                $('bg.btn_' + i + '.desp').setString('');
            }

            TouchUtil.setOnclickListener($('bg.btn_close'), function () {
                self.parent.removeFromParent();
            });

            //进行签到
            TouchUtil.setOnclickListener($('bg.btn_sign'), function () {
                network.send(1003, {userid: gameData.uid+"", gameid: SignInCfg.gameid, configid: 'sevendays', clientversion: "0"})
            });

            return true;
        },
        
        init: function (data) {
            var signhistory = data['signhistory'];
            for(var i = 0; i < data.signconfig.length; i++){
                (function (k) {
                    var signPerData = data.signconfig[k];
                    if(signPerData['url']){
                        downLoadImage(signPerData['url'],function (texture) {
                            var sp = new cc.Sprite(texture);
                            sp.setScale(0.7);
                            sp.setName('sp');
                            $('bg.btn_' + k).addChild(sp);
                            sp.setLocalZOrder(1);
                            sp.setPosition( $('bg.btn_' + k + '.reward').getPosition());
                            $('bg.btn_' + k + '.desp').setString(signPerData['name']);
                            // if(k != 6)
                            //     $('bg.btn_' + k + '.desp').setVisible(true);
                            // $('bg.btn_' + k + '.duigou').setLocalZOrder(2);
                            if(signhistory.addupcount >= 0){
                                if(k < signhistory.addupcount){
                                    Filter.grayScale($('bg.btn_' + k));
                                    Filter.grayScale(sp);
                                    $('bg.btn_' + k + '.desp').setColor(cc.color(131, 131, 131));
                                    $('bg.btn_' + k + '.title').setColor(cc.color(254, 241, 226));
                                }else{
                                    if(k == signhistory.addupcount){
                                        $('bg.btn_' + k).setTexture('modules/main/hall_common_module/res/sign/img/itembg.png');
                                        $('bg.btn_' + k + '.desp').setColor(cc.color(255, 60, 62));
                                        $('bg.btn_' + k + '.title').setColor(cc.color(225, 143, 143));
                                        var light = new cc.Sprite('modules/main/hall_common_module/res/sign/img/itembg3.png');
                                        light.setName('light');
                                        light.setPosition(cc.p($('bg.btn_' + k).getContentSize().width/2, $('bg.btn_' + k).getContentSize().height/2));
                                        $('bg.btn_' + k).addChild(light, -1);
                                    }else{
                                        $('bg.btn_' + k + '.desp').setColor(cc.color(223, 73, 61));
                                        $('bg.btn_' + k + '.title').setColor(cc.color(253, 241, 226));
                                    }
                                }
                            }
                        })
                    }
                })(i);
            }
        },
        //签到成功
        updateSign: function (signItemData) {
            if(signBaseData){
                if(signBaseData.signhistory.addupcount ||  signBaseData.signhistory.addupcount == 0){
                    // $('bg.btn_' + signBaseData.signhistory.addupcount + '.duigou').setVisible(true);
                    // $('bg.btn_' + signBaseData.signhistory.addupcount + '.bg_black').setVisible(true);
                    var kk = signBaseData.signhistory.addupcount;
                    Filter.grayScale($('bg.btn_' + kk));
                    var sp = $('bg.btn_' + kk).getChildByName('sp');
                    if(sp)  Filter.grayScale(sp);
                    $('bg.btn_' + kk + '.desp').setColor(cc.color(131, 131, 131));
                    $('bg.btn_' + kk + '.title').setColor(cc.color(254, 241, 226));
                }
            }

            var addupcount = signBaseData.signhistory.addupcount;
            var signconfig = signBaseData['signconfig'];
            // itemname itemurl 2项必须
            var signArr = [];
            var cellInfo = {};
            var itemname = '';
            var itemurl = '';
            if(signItemData.items && signItemData.items.length > 0){
                var rewardData = signItemData.items;
                for(var i = 0; i <rewardData.length; i++){
                    if(rewardData[i]['good_type'] == 1) {         //金币
                        itemname = itemname + ' 金币x' + rewardData[i]['val'];

                    } else if(rewardData[i]['good_type'] == 2){     //礼包
                        itemname = itemname + ' 礼包x' + rewardData[i]['val'];

                    } else if(rewardData[i]['good_type'] == 3){     //道具
                        var _item = ItemData.getItemInfoByCode(rewardData[i]['good_code']);
                        itemname = itemname + _item.Name + 'x' + rewardData[i]['val'];

                    }else if(rewardData[i]['good_type'] == 4){      //抽奖券
                        itemname = itemname + ' 抽奖券' + rewardData[i]['val'];

                    }else if(rewardData[i]['good_type'] == 5){      //实物
                        console.log('rewardData good_type' + rewardData[i]['good_type'] + '   实物未处理');

                    }else if(rewardData[i]['good_type'] == 6){      //房卡+钻石
                        if(rewardData[i]['good_code'] == 'diamond'){
                            itemname = itemname + ' 钻石x' + rewardData[i]['val'];
                        }else if(rewardData[i]['good_code'] == 'roomcard'){
                            itemname = itemname + ' 房卡x' + rewardData[i]['val'];
                        }else
                            console.log('rewardData   good_type' + rewardData[i]['good_type'] + " 未处理");

                    }else if(rewardData[i]['good_type'] == 7){      // 红包
                        itemname = itemname + ' 红包x' + rewardData[i]['val'];

                    }else
                        console.log('位置类型');
                }
            }
            cellInfo['itemname'] = itemname;
            cellInfo['itemurl'] = signconfig[addupcount]['url'];

            signArr.push(cellInfo);
            showGoodsTips(signArr);
        },
    });
})(window);